<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>
<div id="app">
    <el-page-header style="background-color: #0095d7;color: white;line-height: 60px" @back="goBack" content="发布商品页面">
    </el-page-header>
    <el-divider></el-divider>
    <!--limit="1" 设置只能选择一张图片-->
    <el-card>
        <el-form>
            <el-form-item label="商品标题" label-width="80px">
                <el-input v-model="product.title"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" label-width="80px">
                <el-input v-model="product.price"></el-input>
            </el-form-item>
            <el-form-item label="商品原价" label-width="80px">
                <el-input v-model="product.oldPrice"></el-input>
            </el-form-item>
            <el-form-item label="商品销量" label-width="80px">
                <el-input v-model="product.saleCount"></el-input>
            </el-form-item>
            <el-form-item label="商品分类" label-width="80px">
                <template>
                    <el-select v-model="product.categoryId" placeholder="请选择">
                        <!--:label控制下拉选项的文本内容,:value代表提交给后端的id值-->
                        <el-option v-for="c in categoryArr" :label="c.name" :value="c.id"></el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item label="商品图片" label-width="80px">
                <el-upload
                        action="/upload"
                        name="picFile"
                        :limit="1"
                        list-type="picture-card"
                        :on-preview="handlePictureCardPreview"
                        :on-success="handleSuccess"
                        :on-remove="handleRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
        </el-form>
        <el-button @click="insert()">发布</el-button>
    </el-card>
</div>
</body>
<!-- import Vue before Element -->
<script src="js/vue.js"></script>
<!-- import JavaScript -->
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                product: {
                    title:"",
                    url:"",
                    price:"",
                    oldPrice: "",
                    saleCount:"",
                    categoryId:""
                },
                categoryArr:[]
            }
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                //当点击删除图片时会执行,file表示要删除的文件
                //file.response代表文件上传成功后服务器响应的文件名
                axios.get("/remove?name="+file.response).then(function () {
                    console.log("服务器图片已经删除!");
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleSuccess(response, file, fileList) {
                //response = file.response = 图片名
                console.log("图片名="+response);
                v.product.url = response;
            },
            insert() {
                //得到图片名,一起提交给服务器
                if (v.product.url=="") {
                    alert("图片不能为空!");
                    return;
                }
                axios.post("/product/insert",v.product).then(function (response) {
                        alert("添加完成!");
                        location.href = "/admin.html";
                })
            },
            goBack() {
                history.back();
            }
        },
        created:function () {
            //自动发送请求获取所有分类信息
            axios.get("/category/select").then(function (response) {
                v.categoryArr = response.data;
            })
        }
    })
</script>
</html>